const http = require("../../../utils/http.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        book: {},
        baseImage: http.baseUrl,
        catalogList: [],
        commentList: [],
        isLike: false,
        loginInfo: {},
        comment: {
            content: '',
            score: 3
        },
        showCommentDialog: false,
        beforeClose(action) {
            return action !== "confirm";
        },
        catalogPageInfo: {
            page: 1,
            limit: 6,
            pages: 0
        },
        commentPageInfo: {
            page: 1,
            limit: 6,
            pages: 0,
            total: 0
        },
        score: 0.0
    },
    getBookInfo() {
        http.get('wx/v1/book/info/' + this.data.book.bookId).then(data => {
            let score = 0.00;
            if (data.scoreCount > 0) {
                score = (data.score / data.scoreCount).toFixed(2)
            }
            this.setData({
                book: {...data},
                score
            })
            http.get('wx/vip/like/check', {bookId: this.data.book.bookId}).then(data => {
                this.setData({
                    isLike: data
                })
            })
        })
    },
    getLoginInfo() {
        http.get('wx/vip/info', {}, false).then(data => {
            this.setData({
                loginInfo: {...data},
                isLogin: true
            })
        }).catch(() => {
            this.setData({
                loginInfo: {},
                isLogin: false
            })
            wx.showToast({title: "请先登录！", icon: "error"})
            wx.reLaunch({url: "/pages/index/index"})
        })
    },
    getCatalogList() {
        this.data.catalogPageInfo.bookId = this.data.book.bookId
        http.get('wx/v1/book/catalog/page', this.data.catalogPageInfo).then(data => {
            this.setData({
                catalogList: [...this.data.catalogList, ...data.records],
                "catalogPageInfo.pages": data.pages
            })
        })
    },
    getCommentList() {
        this.data.commentPageInfo.bookId = this.data.book.bookId
        http.get('wx/v1/book/comment/page', this.data.commentPageInfo).then(data => {
            this.setData({
                commentList: [...this.data.commentList, ...data.records],
                "commentPageInfo.pages": data.pages,
                "commentPageInfo.total": data.total
            })
        })
    },
    scrollToBottomCatalog() {
        if (this.data.catalogPageInfo.page < this.data.catalogPageInfo.pages) {
            this.setData({
                "catalogPageInfo.page": this.data.catalogPageInfo.page + 1
            })
            this.getCatalogList()
        } else {
            wx.showToast({title: "没有更多目录！"})
        }
    },
    scrollToBottomComment() {
        if (this.data.commentPageInfo.page < this.data.commentPageInfo.pages) {
            this.setData({
                "commentPageInfo.page": this.data.commentPageInfo.page + 1
            })
            this.getCommentList()
        } else {
            wx.showToast({title: "没有更多评论！"})
        }
    },
    deleteComment(e) {
        const id = e.currentTarget.dataset.id
        wx.showModal({
            title: "提示",
            content: "确定删除该评论吗?",
            showCancel: true,
            success: res => {
                if (res.confirm) {
                    http.delete('wx/vip/comment/delete/' + id).then(() => {
                        wx.showToast({title: "删除成功!", icon: "success"})
                        this.setData({
                            commentList: []
                        })
                        this.getCommentList()
                    })
                }
            }

        })
    },
    showComment() {
        this.setData({
            showCommentDialog: true
        })
    },
    getContent(e) {
        this.setData({
            "comment.content": e.detail
        })
    },
    getScore(e) {
        this.setData({
            "comment.score": e.detail
        })
    },
    dialogClose() {
        this.setData({
            comment: {
                content: "",
                score: 3
            }
        })
    },
    dialogConfirm() {
        if (!this.data.comment.content) {
            wx.showToast({title: "内容不能为空！", icon: "error"});
            return false;
        }
        this.data.comment.bookId = this.data.book.bookId
        http.post('wx/vip/comment/save', this.data.comment).then(() => {
            wx.showToast({title: "发布成功！", icon: "success"});
            this.setData({
                comment: {
                    content: "",
                    score: 3
                },
                showCommentDialog: false,
                commentList: []
            })
            this.getCommentList()
        })
    },
    likeBook() {
        http.post('wx/vip/like/save', {bookId: this.data.book.bookId}).then(() => {
            wx.showToast({title: `${this.data.isLike ? '取消成功' : '收藏成功'}`})
            this.getBookInfo()
        })
    },
    readBook() {
        http.get('wx/vip/catalog/book/' + this.data.book.bookId).then(data => {
            wx.navigateTo({url: "/pages/book/read/read?catalogId=" + data.catalogId})
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            "book.bookId": options.bookId
        }, () => {
            this.getLoginInfo()
            this.getBookInfo()
            this.getCatalogList()
            this.getCommentList()
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
